/**
 * @file:          float2str.c
 * @brief:         
 * @details:       
 * @author:        wjh
 * @date created:  2025.07.20
 * @version:       1.0.0
 * @par copyright (c):
 *   
 * @par history (desc):
 *   version:1.0.0, wjh, 2025.07.20,03:14:40
 */


/* Include Files **************************************************************/
#include "float2str.h"
#include <string.h>
#include "base_math.h"
#include <stdio.h>

/* Global Variable Define *****************************************************/
char float_str_buff[FLOAT2STR_CHANNEL_SIZE][15];

/* Function Define ************************************************************/
char *float2str(float data_float, uint16_t disp_bits)
{
    int32_t f32_a;
    int32_t f32_b;
    uint32_t disp_gains = 1;
    static uint16_t num_loop = 0;

    num_loop = (num_loop + 1) % FLOAT2STR_CHANNEL_SIZE;

    for (uint16_t i = 0; i < disp_bits; i++)
    {
        disp_gains *= 10;
    }

    f32_a = (int32_t)abs_bm(data_float);
    f32_b = (int32_t)((abs_bm(data_float) - f32_a) * disp_gains);

    switch (disp_bits)
    {
    case 1:
        sprintf(float_str_buff[num_loop], "-%ld.%01ld", f32_a, f32_b);
        break;
    case 2:
        sprintf(float_str_buff[num_loop], "-%ld.%02ld", f32_a, f32_b);
        break;
    case 3:
        sprintf(float_str_buff[num_loop], "-%ld.%03ld", f32_a, f32_b);
        break;
    case 4:
        sprintf(float_str_buff[num_loop], "-%ld.%04ld", f32_a, f32_b);
        break;
    case 5:
        sprintf(float_str_buff[num_loop], "-%ld.%05ld", f32_a, f32_b);
        break;
    case 6:
        sprintf(float_str_buff[num_loop], "-%ld.%06ld", f32_a, f32_b);
        break;
    default:
        break;
    }

    if (data_float >= 0)
    {
        strcpy(float_str_buff[num_loop], float_str_buff[num_loop] + 1);
    }

    return float_str_buff[num_loop];
}